
    /**
     * 无限极分类，V190701
     * @param container
     * @param selectData
     * @param getChildren
     * @param onAppend 当追加SELECT完毕
     */
    var InfiniteSelect = function(){

    };

    /**
     * 创建select
     */
    InfiniteSelect.create = function(container, id, getChildren, onAppend, defaultChild){
        if(!arguments[4])defaultChild = 0;

        //创建SELECT对象
        var select = $('<select></select>');
        container.append(select);
        onAppend(select, container.children('select').length-1);

        //默认第一个选项
        var option = $('<option>请选择</option>');
        option.attr('value', -1);
        $(select).append(option);

        getChildren(id, function (topData) {
            //创建OPTION
            for(var i=0; i<topData.length; i++){
                var option = $('<option>'+topData[i].name+'</option>');
                option.attr('value', topData[i].id);
                if(topData[i].id == defaultChild){
                    option.attr('selected', true);
                }
                $(select).append(option);
            }

            //设置改变事件
            select.on('change', function () {
                var id = $(this).val();
                InfiniteSelect.removeSelectAfter(container, $(this));
                appendChildSelect(id);
                container.attr('cid', id);
            });

            var appendChildSelect = function (id) {
                getChildren(id, function (children) {
                    if(children.length > 0){
                        InfiniteSelect.create(container, id, getChildren, onAppend);
                    }
                });
            };
        });

    }


    /**
     * 删除选择的SELECT后的所有SELSECT
     * @param container
     * @param seleObj
     */
    InfiniteSelect.removeSelectAfter = function(container, seleObj){
        var selects = container.children('select');
        var temp = 0;
        //先找出选择的是第几个SELECT
        for(var i=0; i<selects.length; i++){
            if(selects[i] == seleObj.get(0)){
                temp = i;
            }
        }
        for(var i=temp+1; i<(selects.length); i++){
            selects[i].remove();
        }
    };

    /**
     * 递归初始化已选择分类
     * @param id 项目ID
     */
    InfiniteSelect.createAll = function(container, getChildren, onAppend){
        InfiniteSelect.create(container, 0, getChildren, onAppend);
    }

    /**
     * 根据IDS初始化SELECT
     * @param container
     * @param ids
     * @param getChildren
     * @param onAppend
     */
    InfiniteSelect.createByIds = function(container, ids, getChildren, onAppend){
        for(var i=0; i<ids.length; i++){
            var parentId = (i>0) ? ids[i-1] : 0;
            InfiniteSelect.create(container, parentId, getChildren, onAppend, ids[i]);
        }
        //如果有只分类，再显示一级
        var lastId = ids[ids.length-1];
        getChildren(lastId, function (children) {
            if(children.length > 0){
                InfiniteSelect.create(container, lastId, getChildren, onAppend);
            }
        });
    };

    InfiniteSelect.getSelectedId = function(selects){
        for(var i=(selects.length-1); i>=0; i--){
            if(parseInt(selects[i].value) > 0){
                return parseInt(selects[i].value);
            }
        }
        return 0;
    }

    
/** 调用示例
*
*
*
    //准备
    $(document).ready(function(){

        console.log(parentIds);

        var getChildren = function(id, onFinished){
            $.ajax({
                url : "<?php echo Url::to(["/v1/article/get-children-categories"])?>",
                data : {id : id,},
                dataType : 'json',
                success : function (data) {
                    onFinished(data);
                }
            });
        };

        var onAppend = function (select, n) {
            if(n > 0){
                select.css('margin-left', '5px');
            }
        };

        if(parentIds.length > 0){
            InfiniteSelect.createByIds($('#cate_cont'), parentIds, getChildren, onAppend);
        }else{
            InfiniteSelect.create($('#cate_cont'), 0, getChildren, onAppend);
        }

    });
*
* */

